/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hello;

import java.util.Vector;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Display;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.StringItem;
import javax.microedition.lcdui.TextField;
import javax.microedition.lcdui.Ticker;
import javax.microedition.midlet.*;

/**
 *
 * @author Guillaume
 */
public class Client extends MIDlet implements CommandListener {

    private Form waitScreen;
    public Form connectForm;
    private TextField serverIP;
    private Form hangmanForm;
    private Ticker gameRules;
    private TextField guessWord;
    private StringItem announce;
    private StringItem sentCharacters;
    private Command connectCommand, sendCommand, endAlertCommand;
    private Command exitCommand, restartCommand;
    public Display display;
    private Handler myHandler;
    private Vector charactersSent = new Vector(30);

    public static void main(String[] args) {
        new Client();

    }

    public Client() {
        display = Display.getDisplay(this);
        connectForm = new Form("Connect to hangman server");
        serverIP = new TextField("Server IP", "127.0.0.1", 50, 0);

        hangmanForm = new Form("Hangman");
        guessWord = new TextField("Guess word:", "", 100, TextField.ANY);
        guessWord.setInitialInputMode("MIDP_LOWERCASE_LATIN");
        announce = new StringItem("announce", "");
        sentCharacters = new StringItem("sent characters", "");
        gameRules = new Ticker("Try to guess the correct word by sending letters");

        connectCommand = new Command("Connect", Command.SCREEN, 1);
        exitCommand = new Command("Exit", Command.SCREEN, 2);
        sendCommand = new Command("Send", Command.SCREEN, 1);
        restartCommand = new Command("Restart", Command.SCREEN, 2);

        waitScreen = new Form("wait screen");
    }

    protected void destroyApp(boolean unconditional) throws MIDletStateChangeException {
        // Do nothing for the moment
        // Maybe close the socket after
    }

    protected void pauseApp() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    protected void startApp() throws MIDletStateChangeException {
        connectForm.addCommand(connectCommand);
        connectForm.addCommand(exitCommand);
        connectForm.append(serverIP);
        connectForm.setCommandListener(this);

        hangmanForm.addCommand(sendCommand);
        hangmanForm.addCommand(exitCommand);
        hangmanForm.addCommand(restartCommand);
        hangmanForm.setTicker(gameRules);
        hangmanForm.append(guessWord);
        hangmanForm.append(sentCharacters);
        hangmanForm.append(announce);
        hangmanForm.setCommandListener(this);

        display.setCurrent(connectForm);
    }

    public void commandAction(Command c, Displayable d) {
        if (c == exitCommand) {
            try {
                destroyApp(false);
            } catch (MIDletStateChangeException ex) {
                ex.printStackTrace();
            }
            notifyDestroyed();
        } else if (c == connectCommand) {
            myHandler = new Handler(serverIP.getString(), guessWord, announce, this);
            myHandler.start();
            display.setCurrent(waitScreen);
        } else if (c == sendCommand) {
            myHandler.send();
            createSentCharactersList(guessWord.getString().toCharArray());
            guessWord.setString("");
            sentCharacters.setLabel(charactersSent.toString());
        } else if (c == restartCommand) {
            myHandler.closeConnection();
            myHandler = new Handler(serverIP.getString(), guessWord, announce, this);
            myHandler.start();
            charactersSent.removeAllElements();

            hangmanForm();
        } else if (c == endAlertCommand) {
            display.setCurrent(connectForm);
        }
    }

    private void createSentCharactersList(char[] data) {
        int dataSize = data.length;

        for (int i = 0; i < dataSize; i++) {
            if (!charactersSent.contains("" + data[i])) {
                charactersSent.addElement("" + data[i]);
            }
        }
    }

    public void startHangman() {
        charactersSent.removeAllElements();
        hangmanForm();
    }

    public void hangmanForm() {
        guessWord.setString("");
        sentCharacters.setLabel("");
        announce.setLabel("announces");
        display.setCurrent(hangmanForm);
    }
}
